home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / controlstripsample / controlstripsample.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  9.2 KB  |  339 lines

  1. /*
  2.     File:        ControlStrip Sample.c
  3.  
  4.     Contains:    This sample shows how to call the ControlStrip functions
  5.  
  6.     Written by: Matthew Xavier Mora    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/19/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. //------------------------------------------------------------------
  25. #pragma mark Includes
  26. //------------------------------------------------------------------
  27.  
  28. #include <ControlStrip.h>
  29. #include <Gestalt.h>
  30. #include <string.h>
  31. #include <Script.h>
  32. #include <Resources.h>
  33.  
  34. #include "SAL_Public.h"
  35.  
  36. // Key definitions
  37. enum {
  38.     returnKeyCode        = 36,
  39.     tabKeyCode            = 48,
  40.     spaceKeyCode        = 49,
  41.     deleteKeyCode        = 51,    
  42.     escapeKeyCode         = 53,
  43.     clearKeyCode        = 71,
  44.     enterKeyCode         = 76,
  45.     f5KeyCode            = 96,
  46.     f6KeyCode            = 97,
  47.     f7KeyCode            = 98,    
  48.     f3KeyCode            = 99,
  49.     f8KeyCode            = 100,
  50.     f9KeyCode            = 101,    
  51.     f11KeyCode            = 103,
  52.     f13KeyCode            = 105,    
  53.     f14KeyCode            = 107,
  54.     f10KeyCode            = 109,
  55.     f12KeyCode            = 111,
  56.     f15KeyCode            = 113,
  57.     helpKeyCode         = 114,
  58.     homeKeyCode            = 115,
  59.     pageUpKeyCode        = 116,
  60.     forwardDelKeyCode    = 117,
  61.     f4KeyCode            = 118,
  62.     endKeyCode            = 119,
  63.     f2KeyCode            = 120,
  64.     pageDownKeyCode        = 121,    
  65.     f1KeyCode            = 122,
  66.     leftArrowKeyCode    = 123,
  67.     rightArrowKeyCode    = 124,
  68.     downArrowKeyCode    = 125,
  69.     upArrowKeyCode        = 126
  70. };
  71.  
  72.  
  73. #define kNumberofSpecialKeys 32
  74. struct KeyTable {
  75.     short keyCode;
  76.     char  name[14];
  77. };
  78.  
  79. typedef struct KeyTable KeyTable;
  80.  
  81.  
  82. KeyTable KeyCodeTable[kNumberofSpecialKeys] = {
  83.     {escapeKeyCode ,"Escape"},
  84.     {f1KeyCode        ,"F1"},
  85.     {f2KeyCode        ,"F2"},
  86.     {f3KeyCode        ,"F3"},
  87.     {f4KeyCode        ,"F4"},
  88.     {f5KeyCode        ,"F5"},
  89.     {f6KeyCode        ,"F6"},
  90.     {f7KeyCode        ,"F7"},
  91.     {f8KeyCode        ,"F8"},
  92.     {f9KeyCode        ,"F9"},
  93.     {f10KeyCode        ,"F10"},
  94.     {f11KeyCode        ,"F11"},
  95.     {f12KeyCode        ,"F12"},
  96.     {f13KeyCode        ,"F13"},
  97.     {f14KeyCode        ,"F14"},
  98.     {f15KeyCode            ,"F15"},
  99.     {upArrowKeyCode        ,"Up Arrow"},
  100.     {downArrowKeyCode    ,"Down Arrow"},
  101.     {leftArrowKeyCode    ,"Left Arrow"},
  102.     {rightArrowKeyCode    ,"Right Arrow"},
  103.     {helpKeyCode         ,"Help"},
  104.     {homeKeyCode        ,"Home"},
  105.     {pageUpKeyCode        ,"Page Up"},
  106.     {forwardDelKeyCode    ,"Forward Delete"},
  107.     {endKeyCode            ,"End"},
  108.     {pageDownKeyCode    ,"Page Down"},
  109.     {deleteKeyCode        ,"Delete"},
  110.     {returnKeyCode        ,"Return"},
  111.     {enterKeyCode         ,"Enter"},
  112.     {clearKeyCode        ,"Clear"},
  113.     {spaceKeyCode        ,"Space"},
  114.     {tabKeyCode            ,"Tab"}
  115. };
  116.  
  117.  
  118. //------------------------------------------------------------------
  119. #pragma mark Defines
  120. //------------------------------------------------------------------
  121. #define        kDefaultTextSize  9
  122. #define     kMaxSpecs 25        
  123.  
  124. //------------------------------------------------------------------
  125. #pragma mark Globals
  126. //------------------------------------------------------------------
  127.  
  128. Boolean     gHasControlStrip         = false;
  129. Boolean     gCSSupportsUserFont        = false;
  130. Boolean     gCSSupportsUserHotKey    = false;
  131.  
  132. //------------------------------------------------------------------
  133. #pragma mark Prototypes
  134. //------------------------------------------------------------------
  135.  
  136. pascal short DoIdle(EventRecord *evt);
  137. pascal short DoButton(ButtonItemRef me,long m);
  138. pascal short MyUpdate(long m);    
  139.  
  140. #pragma mark -
  141.  
  142. //------------------------------------------------------------------
  143. pascal short DoButton(const ButtonItemRef me,const long refcon)
  144. //------------------------------------------------------------------
  145. {
  146. #pragma unused (me,refcon)
  147.  
  148.     Boolean isVisable = SBIsControlStripVisible();
  149.     
  150.     SBShowHideControlStrip(!isVisable);
  151.     
  152.     return noErr;
  153. }
  154.  
  155. //------------------------------------------------------------------
  156. static Boolean HasControlStrip(void)
  157. //------------------------------------------------------------------
  158. {
  159.     short err;
  160.     long response;
  161.     
  162.     gHasControlStrip         = false;     // make sure the globals are set
  163.     gCSSupportsUserFont     = false;
  164.     gCSSupportsUserHotKey     = false;
  165.  
  166.     if (gSAL_Mac.systemVersion >= 0x0700) {
  167.         err  = Gestalt(gestaltControlStripAttr,&response);
  168.         if (err == noErr) {
  169.             gHasControlStrip         = (response & (1 << gestaltControlStripExists));
  170.             gCSSupportsUserFont     = (response & (1 << gestaltControlStripUserFont));
  171.             gCSSupportsUserHotKey     = (response & (1 << gestaltControlStripUserHotKey));
  172.         }
  173.     } 
  174.     
  175.     return gHasControlStrip;
  176. }
  177.  
  178. //------------------------------------------------------------------
  179.     static SInt8 MyLockHandle(Handle h)
  180. //------------------------------------------------------------------
  181. {
  182.     SInt8 handleState;
  183.     
  184.     handleState = HGetState(h);
  185.     HLock(h);
  186.     return handleState;
  187. }
  188.  
  189. //------------------------------------------------------------------
  190. static IsSpecialKey(short KeyCode ,char *name)
  191. //------------------------------------------------------------------
  192. {
  193.     short i;
  194.     
  195.     for (i = 0 ;i < kNumberofSpecialKeys;i++) {
  196.         if (KeyCodeTable[i].keyCode == KeyCode ) {
  197.             strcpy(name,KeyCodeTable[i].name);
  198.             
  199.             return true;
  200.         }
  201.     }
  202.      
  203.     return false;
  204. }
  205.  
  206.  
  207. //------------------------------------------------------------------
  208.     static char GetCharFromKeyCode(UInt16 keyCode)
  209. //------------------------------------------------------------------
  210. {
  211.     static     UInt32     state = 0;                 // should be 0 first time thru
  212.     Handle     keyboardHandle = nil;            // Handle to keyboard resource if needed
  213.     Ptr      kCharCachePtr;                    // Pointer to kCharCache
  214.     char    theChar = 0x00;                    // the char to return
  215. //    UInt8    handleState;
  216.         
  217.     kCharCachePtr = (Ptr)GetScriptManagerVariable(smKCHRCache);
  218.     
  219.     if (kCharCachePtr == nil ) {                                    // should never happen
  220.         long keyboardID = GetScriptManagerVariable(smKeyScript);
  221.  
  222.         keyboardHandle  = GetResource('KCHR',keyboardID);
  223.         
  224.         if (keyboardHandle) {
  225.             //handleState = MyLockHandle(keyboardHandle); // tech note x says we don't have to lock it
  226.             kCharCachePtr = *keyboardHandle;
  227.         }
  228.     }
  229.     
  230.     if (kCharCachePtr) {
  231.         UInt32     result;                            // result from KeyTranslate
  232.  
  233.         result = KeyTranslate(kCharCachePtr,keyCode,&state);        
  234.         theChar = result & charCodeMask;
  235.     }
  236.     
  237.     if (keyboardHandle) {                    // if we got the resource lets release it
  238.         ReleaseResource(keyboardHandle);    // tech note x does this so we will too.
  239.     }
  240.     
  241.     return theChar;
  242. }
  243.  
  244.  
  245. //------------------------------------------------------------------
  246.     pascal short MyUpdate(long refcon)
  247. //------------------------------------------------------------------
  248. {
  249. #pragma unused (refcon)
  250.  
  251.     Boolean         hotKey;
  252.     unsigned char     keyCode;
  253. //    char             theChar;
  254.     short             modifiers;
  255.     short             err;
  256.     char             name[32];
  257.     
  258.     MoveTo(10,100);
  259.  
  260.     if (gHasControlStrip) {              // if Control strip is installed
  261.         DrawString("\pControl Strip is installed.");
  262.         MoveTo(10,120);
  263.         DrawString("\pClick the button to toggle the state of the control strip.");
  264.         MoveTo(10,140);
  265.         if (gCSSupportsUserHotKey){                    // if we have a version that supports Hot keys
  266.             SBIsShowHideHotKeyEnabled(&hotKey);        // is hotkey enabled?
  267.              
  268.             if (hotKey) {
  269.                 DrawString("\pHot Key is enabled.");
  270.              } else {
  271.                 DrawString("\pHot key is not enabled.");
  272.              }
  273.              MoveTo(10,160);
  274.              
  275.              err = SBGetShowHideHotKey(&modifiers, &keyCode);    // get the hot key combo
  276.              if (err == noErr) {
  277.                  DrawString ("\pHot Key is: ");                    // print the modifiers
  278.                  
  279.                  if (modifiers & cmdKey) {
  280.                      DrawString ("\pCommand + ");
  281.                  }
  282.                  if (modifiers & shiftKey) {
  283.                      DrawString ("\pShift + ");
  284.                  }
  285.                  if (modifiers & optionKey) {
  286.                      DrawString ("\pOption + ");
  287.                  }
  288.                  if (modifiers & controlKey) {
  289.                      DrawString ("\pControl + ");
  290.                  }
  291.                  
  292.                  if (IsSpecialKey(keyCode,name)) {
  293.                     DrawText(name,0,strlen(name));                 
  294.                  } else {
  295.                      char theChar = GetCharFromKeyCode(keyCode);
  296.                      DrawChar(theChar);                                // print the char
  297.                  }
  298.              }
  299.          }
  300.      } else { // if gHasControlStrip
  301.          DrawString("\pControlStrip is not installed.");
  302.      }
  303.      
  304.     return noErr;
  305. }
  306.  
  307.  
  308. //------------------------------------------------------------------
  309.     void main(void)
  310. //------------------------------------------------------------------
  311. {
  312.     short         gMyWindowID;
  313.     short         err;
  314.     Rect         r ;
  315.     short        buttonBottom;
  316.  
  317.     long buttonId = 1;
  318.     
  319.     SAL_InitSimpleApp(2,kSAL_UseStandardMenu);      // Simple App Sets up the Tool Box For us 
  320.     gMyWindowID = SAL_GetDocumentWindow(128,nil);     // Get our stored window 
  321.  
  322.     SAL_SetRectDimensions(&r, 170, 20);            // This sets a rect's size without changing it position
  323.     SAL_SetRectLocation(&r, 10, 30);            // This sets a rect's anchor point
  324.  
  325.     SAL_SetWindowUpdateProc(gSAL_CurrentWindow,MyUpdate ); //gSAL_CurrentWindow for now, its a Kludge
  326.         
  327.     err = SAL_InstallPushButton(0,gSAL_CurrentWindow,"\pToggle Control Strip",&r,0,DoButton,nil);
  328.     buttonBottom = r.bottom;
  329.     
  330.     if (!HasControlStrip()) {            // No control strip so dim the button
  331.         SAL_DisableMe();                  // disables the current button which is the one we just created
  332.     }
  333.  
  334.     SAL_GetPrintArea(gSAL_CurrentWindow,&r);    //Set the print area top coordinate
  335.     r.top = buttonBottom + 1 ;          //so that you don't scroll the button out of view
  336.     SAL_SetPrintArea(gSAL_CurrentWindow,&r);
  337.     SAL_Run();                                 //Let SimpleApp handle the rest
  338. }
  339.